import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import calendar_ex.CalendarDate;
import calendar_ex.CalendarEx;


class YearViewPanel extends ObservingJPanel {
	CalendarEx calendarModel;
	JPanel[] minigrids;
	JPanel[] mglabels;

	public YearViewPanel(CalendarEx model, CalendarController controller) {
		
		super(model, controller);
		this.calendarModel = model;
		this.update();
				
	}

	@Override
	public void update() {
		this.removeAll();
		
		minigrids = new JPanel[12];
		mglabels  = new JPanel[12];
		
		JPanel grid = new JPanel();
		grid.setBackground(Color.WHITE);
		grid.setPreferredSize(new Dimension(280,390));
		GridLayout gl = new GridLayout(4,3);
		gl.setHgap(5);
		gl.setVgap(10);
		grid.setLayout(gl);

		String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", 
				"Aug", "Sep", "Oct", "Nov", "Dec"};
		int[] lengths11 = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 
		int[] lengths12 = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 
		int[] lengths;


		//set up month grids
		for (int m = 0; m < 12; m++){
			
			if (calendarModel.getViewDate().getYear() == 2012)
				lengths = lengths12;
			else
				lengths = lengths11;
			
			JButton[] buttons = new JButton[lengths[m]];
			JPanel minigrid = new JPanel();
			minigrid.setPreferredSize(new Dimension(80,70));
			minigrid.setLayout(new GridLayout(0,5));
			

			Calendar today = Calendar.getInstance();

			for(int i = 0; i < lengths[m]; i++) {

				int tm = today.get(Calendar.MONTH);
				int td = today.get(Calendar.DAY_OF_MONTH);
				int ty = today.get(Calendar.YEAR);

				buttons[i] = new JButton(Integer.toString(i+1));
				buttons[i].setBackground(Color.WHITE);
				buttons[i].setOpaque(true);
				buttons[i].setFont(buttons[i].getFont().deriveFont((float)9.0));
				buttons[i].setMargin(new Insets(0,0,0,0));
				buttons[i].setEnabled(false);
				
				if (calendarModel.getViewDate().getYear() == ty && m == tm && i == td) {
    				buttons[i].setBackground(new Color(46, 139, 87));
    			}
								
				minigrid.add(buttons[i]);
			}
			
			

			//month grid + month label combo panel
			JPanel mglabel = new JPanel();
			mglabel.setLayout(new BorderLayout());
			JLabel label = new JLabel(months[m], SwingConstants.CENTER);
			
			YearViewMonthListener yvml = new YearViewMonthListener(m+1);
			minigrid.addMouseListener(yvml);
			minigrid.setBackground(Color.WHITE);
			
			for (JButton b : buttons){
				b.addMouseListener(yvml);
			}

			mglabel.add(label, BorderLayout.NORTH);
			mglabel.add(minigrid, BorderLayout.CENTER);
			mglabel.addMouseListener(yvml);
			mglabel.setBackground(Color.WHITE);
			
			mglabels[m] = mglabel;
			minigrids[m] = minigrid;
			

			grid.add(mglabel);
		}


		this.add(grid);

	}
	
	class YearViewMonthListener implements MouseListener {
				
		int m;
		int d = calendarModel.getViewDate().getDay();
		int y = calendarModel.getViewDate().getYear();
		
		YearViewMonthListener(int month){
			this.m = month;
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			calendarModel.setViewDate(new CalendarDate(d,m,y));
			for (int i = 0; i < 12; i++) {
				mglabels[i].setBackground(Color.WHITE);
				minigrids[i].setBackground(Color.WHITE);
			}
			mglabels[m-1].setBackground(new Color(173, 255, 47));
			minigrids[m-1].setBackground(new Color(173, 255, 47));
			revalidate();
			repaint();
		}
		
		@Override
		public void mousePressed(MouseEvent e) {		
		}
		@Override
		public void mouseClicked(MouseEvent e) {
		}
		@Override
		public void mouseEntered(MouseEvent e) {
		}
		@Override
		public void mouseExited(MouseEvent e) {
		}

		
	}

}